package com.allwees.support.payment.payby.builder;

import com.allwees.bs.core.model.order.entity.OrderItemModel;
import com.allwees.bs.core.model.order.entity.OrderModel;
import com.allwees.core.common.constant.EPlatform;
import com.allwees.core.common.context.SpringContextHolder;
import com.allwees.support.payment.model.PaymentInfo;
import com.allwees.support.payment.payby.configuration.PaybyProperties;
import com.allwees.support.payment.paypal.builder.PurchaseUnitRequestBuilder;
import com.allwees.support.payment.paypal.configuration.PaypalProperties;
import com.google.common.collect.Maps;
import com.payby.gateway.openapi.model.ExternalMoney;
import com.payby.gateway.openapi.request.PlaceOrderRequest;
import com.paypal.orders.ApplicationContext;
import com.paypal.orders.OrderRequest;
import com.paypal.orders.PurchaseUnitRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PlaceOrderRequestBuilder {
    private static PaybyProperties paybyProperties = SpringContextHolder.getBean(PaybyProperties.class);

    public static OrderRequest buildEmpty() {
        return new OrderRequest();
    }

    public static PlaceOrderRequest build(PaymentInfo paymentInfo) {
        PlaceOrderRequest request = new PlaceOrderRequest();
        // Merchant order number Required
        request.setMerchantOrderNo(paymentInfo.getOrderUuid());
        // Product name Required
        request.setSubject("From Allwees paying");
        // Order totalAmount Required
        ExternalMoney totalAmount = new ExternalMoney(paymentInfo.getOrder().getAmt(), "AED");
        request.setTotalAmount(totalAmount);
        Map<String,String> params = Maps.newHashMap();
        params.put("appId", paybyProperties.getAppId());
        params.put("iapDeviceId", paymentInfo.getDeviceId());
        request.setPaySceneParams(params);
        if (!paymentInfo.getPlatform().isApp()) {
            request.setPaySceneCode("PAYPAGE");
            Map<String, String> paySceneParams = new HashMap<String, String>();
            paySceneParams.put("redirectUrl", paybyProperties.getReturnURL());
            request.setPaySceneParams(paySceneParams);
        } else {
            request.setPaySceneCode("INAPP");
        }
        request.setNotifyUrl(paybyProperties.getPayResultNotifyURL());

        return request;
    }

    public static List<PurchaseUnitRequest> buildPurchaseUnitRequests(OrderModel order, List<? extends OrderItemModel> orderItems) {
        return PurchaseUnitRequestBuilder.build(order, orderItems);
    }

    public static ApplicationContext buildApplicationContext(String orderUuid, EPlatform platform, PaypalProperties paypal) {
        return new ApplicationContext()
//                .brandName("EXAMPLE INC")
                .landingPage("BILLING")
                .cancelUrl(urlParams(paypal.getCancelURL(), orderUuid, platform))
                .returnUrl(urlParams(paypal.getReturnURL(), orderUuid, platform))
                .userAction("CONTINUE")
//                .shippingPreference("SET_PROVIDED_ADDRESS");
                ;
    }

    private static String urlParams(String url, String orderUuid, EPlatform platform) {
        return url + "?orderUuid=" + orderUuid + "&platform=" + platform.name();
    }
}
